#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace KLib.Entities
{
    [Obsolete("Replaced by the SceneGraph Class",false)]
    public class EntityManager : KLib.DrawableGameComponent
    {
        #region Fields
        List<Entity> entities = new List<Entity>();
        List<Entity> onScreenEntities = new List<Entity>();
        private Graphics.SpriteRenderer spriteBatch;
        Camera.ICameraManagerService camMan;

        #endregion

        #region Properties
        public List<Entity> Entities
        {
            get { return entities; }
        }
        #endregion

        #region Initialization

        public EntityManager(Game game)
            : base(game)
        {
            spriteBatch = new KLib.Graphics.SpriteRenderer(Game.GraphicsDevice, game);
            camMan = (Camera.ICameraManagerService)Game.Services.GetService(typeof(Camera.ICameraManagerService));
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        #endregion

        #region Methods
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            for (int i = 0; i < entities.Count; i++)
            {
                if (!entities[i].Enabled)
                    continue;


                entities[i].Update(gameTime);
                //Populate the list of onScreenEnitites for drawing.
                //I am temporarily adding all entities to the drawing list,
                //however, there should be a test to see if the entitiy is inside the camera view
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            onScreenEntities.Clear();
            for (int i = 0; i < entities.Count; i++)
            {
                if (entities[i].Visible == true)
                    onScreenEntities.Add(entities[i]);
            }

            spriteBatch.Begin(camMan.CurrentCamera.GetCameraMatrix());
            for (int i = 0; i < onScreenEntities.Count; i++)
            {
                onScreenEntities[i].Draw(spriteBatch);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void AddEntity(Entity entity)
        {
            entities.Add(entity);
            if (EntityAdded != null)
                EntityAdded(this,entity);
        }

        public void RemoveEntity(Entity entity)
        {
            if (EntityRemoved != null)
                EntityRemoved(this, entity);

            entities.Remove(entity);
        }

        public void RemoveAndDeleteEntity(Entity entity)
        {
            if (EntityRemoved != null)
                EntityRemoved(this, entity);
            if (EntityDeleted != null)
                EntityDeleted(this, entity);

            entities.Remove(entity);
            entity = null;
            //entity.Dispose();

        }

        public void ClearAllEntities()
        {
            if (AllEntitiesRemoved != null)
                AllEntitiesRemoved(this, null);
            entities.Clear();
        }

        public void ClearAndDeleteAllEntities()
        {
            if (AllEntitiesRemoved != null)
                AllEntitiesRemoved(this, null);
            if (AllEntitiesDeleted != null)
                AllEntitiesDeleted(this, null);

            for (int i = 0; i < entities.Count; i++)
            {
                entities[i] = null;
                //entities[i].Dispose();
            }
            entities.Clear();
        }
        #endregion

        #region Helper Methods

        #endregion

        #region Events
        public delegate void EntityAddRemoveHandler(object sender, Entity entity);
        public event EntityAddRemoveHandler EntityAdded;
        public event EntityAddRemoveHandler EntityRemoved;
        public event EntityAddRemoveHandler EntityDeleted;
        public event EventHandler AllEntitiesRemoved;
        public event EventHandler AllEntitiesDeleted;
        #endregion
    }
}